#include <cstdio>
#include <algorithm>

using namespace std; 

#define T 1001
#define M 101

int time[M], value[M]; 
int t, m; 

int f[T]; 


int main()
{
	int i, j; 
	
	//freopen("p1048.in", "r", stdin); 
	scanf("%d %d", &t, &m); 
	for (i=0; i<m; i++) {
		scanf("%d %d", &time[i], &value[i]); 
	}
	
	for (i=0; i<m; i++) {
		for (j=t; j>=time[i]; j--) {
			f[j] = max(f[j], f[j-time[i]]+value[i]); 
		}
	}
	printf("%d\n", f[t]); 
	return 0; 
}


